
<script setup lang="ts">
import { ref, reactive, nextTick, markRaw } from 'vue'
import { ElMessage } from 'element-plus'
import * as echarts from 'echarts'
import { getGasEchartsDataApi } from '@/api/gas'
import { getInfraredHistoryApi } from '@/api/infrared'
import fixTime from '@/utils/fixTime'
import { convertTime, formatTime } from '@/utils/common'
// props
const props = defineProps({
  gasData: {
    type: Object,
    default: null
  }
})

// 定义变量
// 控制弹框显隐
const dialogVisible = ref(false)

// 图表数据源
const resultData = ref([] as any[])
// echart实例
const myEcharts = echarts
const chart = ref()
// x轴
let xAxisData: any = []
// 气体具体数据
let coSeriesData:any = []
let so2SeriesData:any = []
let ch4SeriesData:any = []
let smokeSeriesData:any = []
let dustSeriesData: any = []
// echarts数据源和设置
const option = {
  xAxis: {
    type: 'category',
    data: [ '' ]
  },
  yAxis: {
    type: 'value'
  },
  // 显示图例
  legend: {
    data: [ '温度' ]
  },
  tooltip: {
    trigger: 'axis'
  },
  grid: {
    left: '5%',
    right: '3%',
    containLabel: true
  },
  series: [
    {
      name: '温度',
      data: [ 0 ],
      type: 'line',
      smooth: true
    }
  ]
}

const nowTime = ref(new Date())
// 筛选时间
const time = ref( new Date(nowTime.value.getTime() - 24 * 60 * 60 * 1000))
// 时间间隔
const timeInterval = ref('1')
// 时间单位
const timeUnit = ref('hours')

let yard = ref(0)

// 图标数据变量定义
let coData:any = ref([])
let ch4Data:any = ref([])
let smokeData:any = ref([])
let so2Data:any = ref([])
let dustData: any = ref([])

const num = ref(0)

// 定义方法
// 展示弹框
const showDialog = async (yardId:number, yardX:string, yardY:string) => {
  dialogVisible.value = true
  yard.value = yardId
  await nextTick()
  await getHistoryData(yardId, yardX, yardY)
  
}

// 获取历史数据
const getHistoryData = async (yardId: number, yardX: string, yardY: string) => {
  console.log('获取历史数据------>', yardX, yardY)
  console.log('获取历史数据------>', (yardX + '').split('.')[0], (yardY + '').split('.')[0])
  
  let data = {
    YardId: yardId,
    X: (yardX + '').split('.')[0],
    Y: (yardY + '').split('.')[0],
    TimeStart: time.value,
    TimeEnd: nowTime.value,
    TimeInterval: '1 hours'
  }
  let res = await getInfraredHistoryApi(data)
  option.series[0].data = []
  option.xAxis.data = []
  res.forEach((item: { x: number, y: number, temperatureMax: number, recordTime :string}) => {
    item.recordTime = formatTime(item.recordTime)
    option.xAxis.data.push(item.recordTime)
    option.series[0].data.push(item.temperatureMax)
  })
  console.log('历史数据---->', res)
  initChart()
}

// 初始化Echarts图表
const initChart = () => {
  console.log('echarts的Option----->', option)
  if (chart.value) {
    chart.value.dispose()
  }
  nextTick(() => {
    
    chart.value = markRaw(myEcharts.init(document.getElementById('infaredContainer')))
    chart.value.setOption(option)
  })
}
// 销毁Echarts
const destroyEchart = () => {
  timeInterval.value = '1'
  timeUnit.value = 'hours'
  chart.value.dispose()
}

// 查询设备信息
// const queryDevice = async () => {
//   if (!time.value[0] || !time.value[1] || !timeInterval.value || !timeUnit.value) {
//     ElMessage({
//       message: '请完整输入查询条件！！！',
//       type: 'warning'
//     })
//     return
//   }
//   let data = {
//     startTime: fixTime(time.value[0]),
//     endTime: fixTime(time.value[1]), 
//     BarYardCode: 'aaa', 
//     BarSectionCode: 'aaa',
//     timeInterval: timeInterval.value + ' ' + timeUnit.value
//   }
//   // console.log('气体历史数据---->', data)
//   // fixXAxis(time.value)
//   let res = await getGasEchartsDataApi(data)
//   resultData.value = res.result
//   res.result.forEach((item: any) => {
//     console.log('气体历史数据---->', item.key)
//     switch (item.key) {
//     case 'CO': 
//       coData.value = item.safetyDetailStatisticsDtos
//       console.log('一氧化碳历史数据---->', coData.value, item.safetyDetailStatisticsDtos)
//       break
//     case 'CH4':
//       ch4Data.value = item.safetyDetailStatisticsDtos
//       console.log('甲烷历史数据---->', ch4Data.value, item.safetyDetailStatisticsDtos)
//       break
//     case 'dust':
//       dustData.value = item.safetyDetailStatisticsDtos
//       console.log('粉尘历史数据---->', dustData.value, item.safetyDetailStatisticsDtos)
//       break
//     case 'smoke':
//       smokeData.value = item.safetyDetailStatisticsDtos
//       console.log('烟感历史数据---->', smokeData.value, item.safetyDetailStatisticsDtos)
//       break
//     case 'O2':
//       so2Data.value = item.safetyDetailStatisticsDtos
//       console.log('氧气历史数据---->', so2Data.value, item.safetyDetailStatisticsDtos)
//       break
//     }
//   })
// }

// 清除查询数据
const clearQueryData = () => {
  timeInterval.value = ''
  time.value[0] = new Date(nowTime.value.getTime() - 24 * 60 * 60 * 1000)
  time.value[1] = new Date()
  timeUnit.value = ''
}

// 确认按钮
const confirm = () => {
  timeInterval.value = '1'
  timeUnit.value = 'hours'
  dialogVisible.value = false

}

// 毫秒数转日期
const millisecondsToDate = (time:number) => {
  let datetime = new Date(time)
  let year = datetime.getFullYear()
  let month = datetime.getMonth()
  let date = datetime.getDate()
  let hour = datetime.getHours()
  let minute = datetime.getMinutes()
  let second = datetime.getSeconds()

  let result1 = year +
    '-' +
    ((month + 1) >= 10 ? (month + 1) : '0' + (month + 1)) +
    '-' +
    ((date + 1) < 10 ? '0' + date : date) +
    ' ' +
    ((hour + 1) < 10 ? '0' + hour : hour) +
    ':' +
    ((minute + 1) < 10 ? '0' + minute : minute) +
    ':' +
    ((second + 1) < 10 ? '0' + second : second)

  let result2 = year +
    '-' +
    ((month + 1) >= 10 ? (month + 1) : '0' + (month + 1)) +
    '-' +
    ((date + 1) < 10 ? '0' + date : date)

  let result = {
    hasTime: result1,
    withoutTime: result2
  }
  return result
}

// 数据清洗 

// 暴露出去的函数
defineExpose({
  showDialog
})
</script>
<template>
  <el-dialog
    v-model="dialogVisible"
    modal-append-to-body
    class="infrared-dialog"
    @close="destroyEchart"
  >
    <template #header>
      <div class="infrared-header-box">
        <div class="title">
          红外历史曲线
        </div>
        <!-- <div class="position-name">
          气体位置{{ props.gasData.name }}
        </div> -->
      </div>
    </template>
    <div class="query-criteria-box">
      <span>{{ convertTime(time)[1] }}</span>&ensp;—&ensp;<span>{{ convertTime(nowTime)[1] }}</span>
    </div>
    <div id="infaredContainer" />
  </el-dialog>
</template>

<style lang="scss" scoped>
.two-item-box {
  display: flex;
  width: 68px;
  height: 40px;
  margin-top: 16px;
  margin-left: 6px;
  background: #f8fafd;
  border: 1px solid rgba($color: #000, $alpha: 0%);
  border-radius: 54px;
  box-shadow: 0 4px 8px 0 rgb(166 184 208 / 40%);
  opacity: 1;
  >div {
    width: 24px;
    height: 24px;
    margin-top: 8px;
    margin-left: 8px;
    line-height: 24px;
    text-align: center;
    background: linear-gradient(180deg, #54aafe 0%, #4482ed 100%);
    border-radius: 24px;
    box-shadow: 0 2px 4px 0 #bbd2ff;
    opacity: 1;
  }
  .o2-box {
    margin-left: 4px;
  }
}
.two-item-box:hover {
  cursor: pointer;
}
.first-son {
  margin-left: 50px;
}
.second-son {
  margin-left: 1192px;
}
.btn-box {
  display: flex;
  margin-left: 30px;

  /* border: 1px solid red; */
  >div {
    width: 44px;
    height: 20px;
    margin-top: 6px;
    font-weight: 600;
    font-size: 14px;
    font-family: PingFang, sans-serif;
    line-height: 20px;
    text-align: center;
    background-clip: text;
  }
  .search-btn {
    color: #2191ff;
  }
  .search-btn:hover {
    cursor: pointer;
  }
  .clear-btn {
    width: 95px;
  }
  .clear-btn:hover {
    cursor: pointer;
  }
}
.icon-divider {
  width: 0 !important;
  height: 12px !important;
  margin-top: 10px !important;
  border: 1px solid #a9aeb8 !important;
}
#infaredContainer {
  width: 720px;
  height: 430px;
}
</style>
<style lang="scss">
.query-criteria-box {
  display: flex;
  .infrared-time {
    width: 100px;

    /* border: 1px solid red; */
  }
  .infrared-interval {
    width: 200px;
    margin-left: 20px;

    /* border: 1px solid red; */
  }
  .infrared-unit {
    width: 150px;
    margin-left: 20px;

    /* border: 1px solid red; */
  }
}
</style>
